( win => {
    if( win.iCopy ){
        win.iCopy.begin();
    } else {
        const task = {

            isWork: false, //是否在工作状态
            target: null, //复制目标节点
            className: 'iCopy-highlight', //目标选中高亮

            /**
             * 开始运行
             */
            begin (){
                if( !task.isWork ){
                    task.isWork = true;
                    task.bindEvent();
                };
            },

            /**
             * 绑定事件
             */
            bindEvent (){
                document.addEventListener('mouseover', task.onMouseOver);
                document.addEventListener('mouseout', task.onMouseOut);
                document.addEventListener('click', task.onClick);
            },

            /**
             * 解绑事件
             */
            unbindEvent (){
                document.removeEventListener('mouseover', task.onMouseOver);
                document.removeEventListener('mouseout', task.onMouseOut);
                document.removeEventListener('click', task.onClick);
            },

            /**
             * 鼠标移入时高亮
             */
            onMouseOver ({ target }){
                target.classList.add( task.className );
            },
        
            /**
             * 鼠标移出时取消高亮
             */
            onMouseOut ({ target }){
                target.classList.remove( task.className );
            },

            /**
             * 目标被单击时触发
             */
            onClick ( e ){
                e.preventDefault();
                task.unbindEvent();
                task.isWork = false;
                task.copyText( e.target );
                e.target.classList.remove( task.className );
            },

            /**
             * 复制目标文本
             */
            copyText ( target ){
                let copy = function (e) {
                    e.preventDefault();
                    if (e.clipboardData) {
                        e.clipboardData.setData('text/plain', target.innerText);
                        task.showNotice( 'success', '复制成功' );
                    } else if (window.clipboardData) {
                        window.clipboardData.setData('Text', target.innerText);
                        task.showNotice( 'success', '复制成功' );
                    } else {
                        task.showNotice( 'failed', '复制失败' );
                    };
                };

                window.addEventListener( 'copy', copy );
                document.execCommand( 'copy' );
                window.removeEventListener( 'copy', copy );
            },

            /**
             * 复制结果回显
             */
            showNotice ( type, msg ){
                let text = document.createElement( 'span' );
                let mask = document.createElement( 'div' );
                let main = document.createElement('div');
                
                text.innerText = msg;
                text.classList.add('iCopy-notices__message');
                mask.setAttribute( 'id', 'iCopy-notices' );
                main.classList.add('iCopy-notices__layout', type);

                main.appendChild( text );
                mask.appendChild( main );
                document.body.appendChild( mask );

                setTimeout(() => {
                    main.classList.add('visible');
        
                    setTimeout(() => {
                        main.classList.remove('visible');
        
                        setTimeout(() => {
                            document.body.removeChild( mask );
                        }, 300);
                    }, 3000);
                }, 300);
            }
        };

        win.iCopy = task;
        win.iCopy.begin();
    };
})( window );